package com.ccx.coachsite.ui.own;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;

import com.ccx.coachsite.CoachSiteMainActivity;
import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.Account;
import com.ccx.coachsite.bus.AccountMgr;
import com.ccx.coachsite.bus.SharedKey;
import com.ccx.coachsite.ui.login.LoginActivity;
import com.ccx.coachsite.ui.login.LoginHelper;
import com.ccx.coachsite.ui.login.RegisterActivity;
import com.ccx.coachsite.util.SharedPreferencesUtil;
import com.ccx.coachsite.util.StringUtil;
import com.ccx.coachsite.versionupdate.SoftUpdateTask;

public class OwerSettingView extends OwerBaseView {
	public static final String TAG = "OwerSettingView";

	private View mBtnRepassword;
	private View mBtnSuggest;
	private View mBtnSocre;
	private View mBtnUpdate;
	private View mBtnUserInfo;
	private View mBtnLoginout;
	private View mBtnAbout;
	
	public OwerSettingView(Context context, OwerMainBase superView) {
		super(context, superView);

		mBtnRepassword = findViewById(R.id.ower_more_btn_repassword);
		mBtnRepassword.setOnClickListener(new OnRepasswordListener());

		mBtnSuggest = findViewById(R.id.ower_more_btn_suggest);
		mBtnSuggest.setOnClickListener(new OnSuggestListener());

		mBtnSocre = findViewById(R.id.ower_more_btn_score);
		mBtnSocre.setOnClickListener(new OnSocreListener());

		mBtnUpdate = findViewById(R.id.ower_more_btn_update);
		mBtnUpdate.setOnClickListener(new OnUpdateListener());

		mBtnUserInfo = findViewById(R.id.ower_more_btn_userinfo);
		mBtnUserInfo.setOnClickListener(new OnUserInfoListener());

		mBtnLoginout = findViewById(R.id.ower_more_btn_loginout);
		mBtnLoginout.setOnClickListener(new OnLoginoutListener());
		
		mBtnAbout = findViewById(R.id.ower_more_btn_about);
		mBtnAbout.setOnClickListener(new OnAboutListener());
		
		String LoginType = SharedPreferencesUtil.getValue(context, SharedKey.Shared_Login_Type, "");
		if(!StringUtil.isEmpty(LoginType))
		{
			int lt = Integer.parseInt(LoginType);
			if(lt >= 1)
			{
				mBtnRepassword.setVisibility(View.GONE);
			}
		}
	}

	private void loginout()
	{
		OwerProvider.getInstance().loginOut(mContext);
		((CoachSiteMainActivity)mContext).refreshMainMenu();
	/*
//		Activity act = (Activity)mContext;
//		act.setResult(1);
//		act.finish();
		
		SharedPreferencesUtil.setValue(mContext,
				SharedKey.Shared_UserId_Key, "");
		SharedPreferencesUtil.setValue(mContext,
				SharedKey.Shared_Mobile_Key, "");
		SharedPreferencesUtil.setValue(mContext,
				SharedKey.Shared_Password_Key, "");
		SharedPreferencesUtil.setValue(mContext,
				SharedKey.Shared_Login_Type, "");
		SharedPreferencesUtil.setValue(mContext,
				SharedKey.Shared_Weibo_Id, "");
		SharedPreferencesUtil.setValue(mContext,
				SharedKey.Shared_Weibo_Name, "");
		
		Account ac = new Account();
		AccountMgr.saveAccountInfo(mContext, ac);
//		
//		Intent intent=new Intent(mContext, LoginActivity.class);
//		mContext.startActivity(intent);
		CoachSiteMainActivity activity = (CoachSiteMainActivity)mContext;
		activity.showChoosedView(CoachSiteMainActivity.APP_VIEW);
		*/
	}
	
	class OnUserInfoListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (!LoginHelper.requireLogin(mContext))
				return;
			
			Intent intent=new Intent(mContext,RegisterActivity.class);
			//intent.putExtra(DownloadItem.EXTRA_ME,item);
			intent.putExtra("modifytype",1);
			mContext.startActivity(intent);
		}
	}

	class OnLoginoutListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			
			if (!LoginHelper.requireLogin(mContext))
				return;
			
			AlertDialog.Builder renameDialog=new AlertDialog.Builder(mContext);
			renameDialog.setTitle("确认注销吗？")
			.setPositiveButton(R.string.common_button_confirm, new DialogInterface.OnClickListener() {	
				@Override
				public void onClick(DialogInterface dialog, int which) {
					loginout();
				}
			})
			.setNegativeButton(R.string.common_button_cancell, new DialogInterface.OnClickListener() {	
				@Override
				public void onClick(DialogInterface dialog, int which) {
					
				}
			})
			.create().show();	
		}
	}

	class OnRepasswordListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (!LoginHelper.requireLogin(mContext))
				return;
			mSuperView.SwitchView(OwerViewKey.ll_personal_more_repassword);
		}
	}
	
	class OnAboutListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			mSuperView.SwitchView(OwerViewKey.ll_personal_more_about);
		}
	}

	class OnSuggestListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			mSuperView.SwitchView(OwerViewKey.ll_personal_more_suggest);
		}
	}

	class OnUpdateListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			SoftUpdateTask task = new SoftUpdateTask(mContext, true);
			task.execute();
		}
	}

	class OnSocreListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			mSuperView.SwitchView(OwerViewKey.ll_personal_more_score);
		}
	}
}
